/* eslint-disable no-unused-vars */
const { Controller } = require('egg');


class EaController extends Controller {

  /**
   * @return {object} 登录
   */
  async login() {
    this.ctx.body = { code: 200 };
  }

  /**
   * @return {object} 学籍信息
   */
  async sInfo() {
    try {
      const table = await this.service.sInfo.table();
      this.ctx.body = {
        code: 200,
        data: {
          table,
        },
      };
    } catch (err) {
      console.log(err);
      this.ctx.body = {
        code: 500,
        msg: '数据获取异常，请稍后重试或向客服反馈',
      };
    }
  }

  /**
   * @return {object} 学期成绩
   */
  async grade() {
    try {
      const { term } = this.ctx.data;
      const { table, header } = await this.service.grade.table(term || 'all');

      this.ctx.body = {
        code: 200,
        data: {
          table,
          header,
        },
      };
    } catch (err) {
      console.log(err);
      this.ctx.body = {
        code: 500,
        msg: '成绩数据获取异常，请稍后重试或向客服反馈',
      };
    }
  }

  /**
   * @return {object} 成绩占比详情
   */
  async gradeProportion() {
    try {
      const { fromLink } = this.ctx.data;
      const table = await this.service.grade.proportion(fromLink);
      this.ctx.body = {
        code: 200,
        data: {
          table,
        },
      };
    } catch (err) {
      console.log(err);
      this.ctx.body = {
        code: 500,
        msg: '成绩占比详情获取异常，请稍后重试或向客服反馈',
      };
    }
  }

  /**
   * @return {object} 学期课程表
   */
  async course() {
    try {
      const { term } = this.ctx.data;
      const { table, time, note, status } = await this.service.course.table(term);
      // console.log('table', JSON.stringify(table, null, 2));

      this.ctx.body = {
        code: 200,
        data: {
          table,
          time,
          note,
        },
      };
    } catch (err) {
      console.log(err);
      this.ctx.body = {
        code: 500,
        msg: '课表获取异常，请稍后重试或向客服反馈',
      };
    }
  }

  /**
   * @return {object} 课程表--班级
   */
  async courseClass() {
    try {
      const { term, yx, nj, zy, name } = this.ctx.data;
      const { table, time, note } = await this.service.course.Ctable(term, yx, nj, zy, name);
      // console.log('table', JSON.stringify(table, null, 2));

      this.ctx.body = {
        code: 200,
        data: {
          table,
          time,
          note,
        },
      };
    } catch (err) {
      console.log(err);
      this.ctx.body = {
        code: 500,
        msg: '课表获取异常，请稍后重试或向客服反馈',
      };
    }
  }

  /**
   * @return {object} 课程表--教师
   */
  async courseTeacher() {
    try {
      const { term, yx, name } = this.ctx.data;
      const { table, time, note } = await this.service.course.Ttable(term, yx, name);
      // console.log('table', JSON.stringify(table, null, 2));

      this.ctx.body = {
        code: 200,
        data: {
          table,
          time,
          note,
        },
      };
    } catch (err) {
      console.log(err);
      this.ctx.body = {
        code: 500,
        msg: '课表获取异常，请稍后重试或向客服反馈',
      };
    }
  }

  /**
   * @return {object} 课程表--教室
   */
  async courseRoom() {
    try {
      const { term, xq, jxl, name } = this.ctx.data;
      const { table, time, note } = await this.service.course.Rtable(term, xq, jxl, name);
      // console.log('table', JSON.stringify(table, null, 2));

      this.ctx.body = {
        code: 200,
        data: {
          table,
          time,
          note,
        },
      };
    } catch (err) {
      console.log(err);
      this.ctx.body = {
        code: 500,
        msg: '课表获取异常，请稍后重试或向客服反馈',
      };
    }
  }

  /**
   * @return {object} 考试安排
   */
  async exam() {
    try {
      const { term } = this.ctx.data;
      const { table, header } = await this.service.exam.table(term);
      this.ctx.body = {
        code: 200,
        data: {
          table,
          header,
        },
      };
    } catch (err) {
      console.log(err);
      this.ctx.body = {
        code: 500,
        msg: '考试安排获取异常，请稍后重试或向客服反馈',
      };
    }
  }

  /**
   * @return {object} 空闲教室
   */
  async emptyRoom() {
    try {
      const { term, zc, week, building, school_id } = this.ctx.data;
      const table = await this.service.emptyRoom.table(term, zc, week, building, school_id);
      this.ctx.body = {
        code: 200,
        data: table,
      };
    } catch (err) {
      console.log(err);
      this.ctx.body = {
        code: 500,
        msg: '空闲教室获取异常，请稍后重试或向客服反馈',
      };
    }
  }

  /**
   * @return {object} 学分修读情况
   */
  async credit() {
    try {
      const table = await this.service.credit.table();

      this.ctx.body = {
        code: 200,
        data: {
          table,
        },
      };
    } catch (err) {
      console.log(err);
      this.ctx.body = {
        code: 500,
        msg: '学分修读数据获取异常，请稍后重试或向客服反馈',
      };
    }
  }

  /**
   * @return {object} 第二课程学分（素拓分）
   */
  async creditExtra() {
    try {
      const { table, header, has } = await this.service.credit.extra();

      this.ctx.body = {
        code: 200,
        data: {
          table,
          header,
          has,
        },
      };
    } catch (err) {
      console.log(err);
      this.ctx.body = {
        code: 500,
        msg: '创新学分获取异常，请稍后重试或向客服反馈', // 根据学校实际叫法来写
      };
    }
  }

  /**
   * @return {object} 执行计划/培养计划（大学修读的课程）
   */
  async plan() {
    try {
      const { table, header } = await this.service.plan.table();

      this.ctx.body = {
        code: 200,
        data: {
          table,
          header,
        },
      };
    } catch (err) {
      console.log(err);
      this.ctx.body = {
        code: 500,
        msg: '培养计划获取异常，请稍后重试或向客服反馈',
      };
    }
  }
}

module.exports = EaController;
